#!/bin/bash

IP=$1
PORT=$2

# 超时时间（防止阻塞）
TIMEOUT=3

# 定义四字命令解析函数
function zk_command() {
  echo -n "$1" | nc -w $TIMEOUT $IP $PORT 2>/dev/null
}

ruok_status=$(zk_command "ruok")
STAT_OUTPUT=$(zk_command "stat")
CONF_OUTPUT=$(zk_command "conf")



stat_version=$(echo "$STAT_OUTPUT" | grep "version:" | awk -F '-' '{print $1}')
stat_mode=$(echo "$STAT_OUTPUT" | grep "Mode:" | awk '{print $2}')
#ZNode总数量(突增可能表示数据泄露或异常写入)
stat_znode=$(echo "$STAT_OUTPUT" | grep "Node count:" | awk -F ':' '{print $2}' | tr -d ' \r')
conf_clientPort=$(echo "$CONF_OUTPUT" | grep -w "clientPort" | awk -F '=' '{print $2}')
conf_electionPort=$(echo "$CONF_OUTPUT" | grep "electionPort" | awk -F '=' '{print $2}')
conf_quorumPort=$(echo "$CONF_OUTPUT" | grep "quorumPort" | awk -F '=' '{print $2}')
conf_dataDir=$(echo "$CONF_OUTPUT" | grep -w "dataDir" | awk -F '=' '{print $2}')
conf_dataDirSize=$(echo "$CONF_OUTPUT" | grep "dataDirSize" | awk -F '=' '{print $2}')
#最大连接数
conf_maxClientCnxns=$(echo "$CONF_OUTPUT" | grep "maxClientCnxns" | awk -F '=' '{print $2}')
#当前活跃连接数
stat_connections=$(echo "$STAT_OUTPUT" | grep "Connections:" | awk '{print $2}')
stat_latency=$(echo "$STAT_OUTPUT" | grep "Latency" | awk -F ':' '{print $2}' | tr -d ' \r')
stat_latency_min=$(echo "$stat_latency" | awk -F '/' '{print $1}')
stat_latency_avg=$(echo "$stat_latency" | awk -F '/' '{print $2}')
stat_latency_max=$(echo "$stat_latency" | awk -F '/' '{print $3}')
#服务启动后接收的总请求数
stat_received=$(echo "$STAT_OUTPUT" | grep "Received:" | awk '{print $2}')
#服务启动后发送的总响应数
stat_sent=$(echo "$STAT_OUTPUT" | grep "Sent:" | awk '{print $2}')
#待处理请求数(持续>0 可能表示服务过载)
stat_outstanding=$(echo "$STAT_OUTPUT" | grep "Outstanding:" | awk '{print $2}')

mntr_status=$(zk_command "mntr")
#节点角色异常切换
zk_server_state=$(echo "$mntr_status" | grep "zk_server_state" | awk '{print $2}')
#ZNode数量突增
zk_znode_count=$(echo "$mntr_status" | grep "zk_znode_count" | awk '{print $2}')
#Watch泄漏
zk_watch_count=$(echo "$mntr_status" | grep "zk_watch_count" | awk '{print $2}')
#临时节点异常
zk_ephemerals_count=$(echo "$mntr_status" | grep "zk_ephemerals_count" | awk '{print $2}')
#数据大小异常
zk_approximate_data_size=$(echo "$mntr_status" | grep "zk_approximate_data_size" | awk '{print $2}')
#文件描述符泄漏
zk_open_file_descriptor_count=$(echo "$mntr_status" | grep "zk_open_file_descriptor_count" | awk '{print $2}')
#Follower 同步延迟
zk_pending_syncs=$(echo "$mntr_status" | grep "zk_pending_syncs" | awk '{print $2}')


echo \
'{
  "ip": "'$IP'",
  "ruok_status": "'$ruok_status'",
  "conf_clientPort": "'$conf_clientPort'",
  "conf_electionPort": "'$conf_electionPort'",
  "conf_quorumPort": "'$conf_quorumPort'",
  "conf_dataDir": "'$conf_dataDir'",
  "conf_dataDirSize": "'$conf_dataDirSize'",
  "conf_maxClientCnxns": "'$conf_maxClientCnxns'",
  "stat_version": "'$stat_version'",
  "stat_mode": "'$stat_mode'",
  "stat_znode": "'$stat_znode'",
  "stat_connections": "'$stat_connections'",
  "stat_latency_min": "'$stat_latency_min'",
  "stat_latency_avg": "'$stat_latency_avg'",
  "stat_latency_max": "'$stat_latency_max'",
  "stat_received": "'$stat_received'",
  "stat_sent": "'$stat_sent'",
  "stat_outstanding": "'$stat_outstanding'",
  "zk_server_state": "'$zk_server_state'",
  "zk_znode_count": "'$zk_znode_count'",
  "zk_watch_count": "'$zk_watch_count'",
  "zk_ephemerals_count": "'$zk_ephemerals_count'",
  "zk_approximate_data_size": "'$zk_approximate_data_size'",
  "zk_open_file_descriptor_count": "'$zk_open_file_descriptor_count'",
  "zk_pending_syncs": "'$zk_pending_syncs'"
}'
